package com.mvvmhabit.org.binding.viewadapter.videoview

import android.media.MediaPlayer
import android.text.TextUtils
import android.widget.MediaController
import android.widget.VideoView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.mvvmhabit.org.binding.command.BindingCommand
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard

@BindingAdapter(value = ["loadVideoUrl"])
fun loadVideoUrl(
    videoView: VideoView,
    loadVideoUrl: String
) {
    videoView.setVideoPath(loadVideoUrl)
    videoView.setMediaController(MediaController(videoView.context))
    videoView.setOnPreparedListener {
        it.start()
    }
}

// 加载JCVideoPlayer 视频播发器地址
@BindingAdapter(value = ["loadJCVideoPlayerUrl", "thumbImageViewUrl"])
fun loadJCVideoPlayerUrl(
    videoView: JCVideoPlayerStandard,
    loadJCVideoUrl: String,
    thumbImageViewUrl: String
) {
    // 加载视频地址
    if (!TextUtils.isEmpty(loadJCVideoUrl)) {
        videoView.setUp(
            loadJCVideoUrl, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, ""
        )
    }
    // 加载默认图片
    if (!TextUtils.isEmpty(thumbImageViewUrl)) {
        Glide.with(videoView.context).load(thumbImageViewUrl).into(videoView.thumbImageView)
    }
}